//
//  LJLRadioPageViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Dark. All rights reserved.
//
#import "LTNetTool.h"
#import "UIImageView+WebCache.h"

#import "LJLRadioPageViewController.h"
#import "LJLRadioPageTVC.h"
#import "LJLRadioPageModel.h"
#import "LJLTopRadioTVC.h"

#import "LJLProvincePageVC.h"

#import "LJLRadioPageLocalVC.h"
#import "ZCSPlayMusicViewController.h"
@interface LJLRadioPageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)UITableView *radioPageTV;

@property(nonatomic, strong)NSMutableArray *recommandRadioDataSource;

@property(nonatomic, strong)NSMutableArray *topRadioDataSource;

@property(nonatomic, strong)NSMutableArray *dataSource;

@property(nonatomic, strong)NSMutableArray *pushDataSource;


@end

@implementation LJLRadioPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.recommandRadioDataSource = [NSMutableArray array];
    self.topRadioDataSource = [NSMutableArray array];
    self.pushDataSource = [NSMutableArray array];
    self.dataSource = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = YES;
    [self createView];
    [self createData];
}
- (void)createView
{
    self.radioPageTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 134 * FITHEIGHT) style:UITableViewStyleGrouped];
    self.radioPageTV.delegate = self;
    self.radioPageTV.dataSource = self;
    
    self.radioPageTV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_radioPageTV];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 120 * FITHEIGHT)];
    headerView.backgroundColor = [UIColor clearColor];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(10 * FITWIDTH, 20 * FITHEIGHT, 80 * FITWIDTH, 80 * FITHEIGHT);
    [leftButton setImage:[[UIImage imageNamed:@"liveLocal"] imageWithRenderingMode:1] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(didClickedLiveLocal) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:leftButton];
    
    UIButton *midLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    midLeftButton.frame = CGRectMake(100 * FITWIDTH, 20 * FITHEIGHT, 80 * FITWIDTH, 80 * FITHEIGHT);
    [midLeftButton setImage:[UIImage imageNamed:@"liveCountry"] forState:UIControlStateNormal];
    [midLeftButton addTarget:self action:@selector(didClickedLiveCountry) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:midLeftButton];
    
    UIButton *midRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    midRightButton.frame = CGRectMake(190 * FITWIDTH, 20 * FITHEIGHT, 80 * FITWIDTH, 80 * FITHEIGHT);
    [midRightButton setImage:[UIImage imageNamed:@"liveProvince"] forState:UIControlStateNormal];
    [midRightButton addTarget:self action:@selector(didClickedLiveProvince) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:midRightButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(280 * FITWIDTH, 20 * FITHEIGHT, 80 * FITWIDTH, 80 * FITHEIGHT);
    [rightButton setImage:[UIImage imageNamed:@"liveNet"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didClickedLiveNet) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:rightButton];
    
    self.radioPageTV.tableHeaderView = headerView;
    self.radioPageTV.separatorStyle = 0;
    [self.radioPageTV registerClass:[LJLRadioPageTVC class] forCellReuseIdentifier:@"LJLradiopageTVC"];
    [self.radioPageTV registerClass:[LJLTopRadioTVC class] forCellReuseIdentifier:@"LJLtopradioTVC"];

    self.radioPageTV.tableFooterView.backgroundColor = [UIColor grayColor];
}

- (void)createData
{
    
    __weak LJLRadioPageViewController *rpvc = self;
    [LTNetTool GetNetWithURL:@"http://live.ximalaya.com/live-web/v1/getHomePageRadiosList?statEvent=pageview%2Ftab%40%E5%8F%91%E7%8E%B0_%E5%B9%BF%E6%92%AD" Body:nil Header:nil Response:0 Success:^(id result)
    {
        NSArray *array = [[result objectForKey:@"result"] objectForKey:@"recommandRadioList"];
        for (NSDictionary *dic in array) {
            LJLRadioPageModel *model = [[LJLRadioPageModel alloc] initWithDic:dic];
            [self.recommandRadioDataSource addObject:model];
        }
        for (NSDictionary *dic in [[result objectForKey:@"result"] objectForKey:@"topRadioList"]) {
            LJLRadioPageModel *model = [[LJLRadioPageModel alloc] initWithDic:dic];
            [self.topRadioDataSource addObject:model];
        }
        [rpvc.radioPageTV reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30 * FITHEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
       
        return 1;
    }
    else
    {
        return self.topRadioDataSource.count;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30 * FITHEIGHT)];
    view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    UIImageView *arrowIV = [[UIImageView alloc] initWithFrame:CGRectMake(10 * FITWIDTH, 12 * FITHEIGHT, 8 * FITWIDTH, 10 * FITHEIGHT)];
    arrowIV.image = [UIImage imageNamed:@"liveRadioCellPoint"];
    [view addSubview:arrowIV];
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(22 * FITWIDTH, 5 * FITHEIGHT, 175 * FITWIDTH, 20 * FITHEIGHT)];
    titleL.textColor = [UIColor whiteColor];
    [view addSubview:titleL];
    if (section == 0) {
        titleL.text = @"推荐电台";
        titleL.font = [UIFont systemFontOfSize:12];
    }
    else if (section == 1)
    {
        titleL.text = @"排行榜";
        titleL.font = [UIFont systemFontOfSize:12];
//        UIButton *moreB = [UIButton buttonWithType:UIButtonTypeCustom];
//        moreB.frame = CGRectMake(295, 5, 80, 20);
//        UILabel *moreL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
//        moreL.text = @"更多";
//        moreL.font = [UIFont systemFontOfSize:12];
//        [moreB addSubview:moreL];
//        UIImageView *arrIV = [[UIImageView alloc] initWithFrame:CGRectMake(40, 0, 20, 20)];
//        arrIV.image = [UIImage imageNamed:@"向右"];
//        [moreB addSubview:arrIV];
//        [view addSubview:moreB];
    }
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 170 * FITHEIGHT;
    }
    else
    {
        return 100 * FITHEIGHT;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LJLRadioPageTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"LJLradiopageTVC"];
        cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
        __weak LJLRadioPageViewController *weak = self;
        cell.rBlock = ^(NSInteger index)
        {
            ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
            play.isRadio = YES;
            NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:weak.recommandRadioDataSource[index], nil];
            play.dataArray = tempArray;
            play.musicIndex = 0;
            play.hidesBottomBarWhenPushed = YES;
            [weak presentViewController:play animated:YES completion:nil];
        };
        if (self.recommandRadioDataSource.count) {
            cell.dataArray = self.recommandRadioDataSource;
        }
        return cell;
    }
    else
    {
        LJLTopRadioTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"LJLtopradioTVC"];
        cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
        cell.model = [self.topRadioDataSource objectAtIndex:indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
        play.isRadio = YES;
        NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:self.topRadioDataSource[indexPath.row], nil];
        play.dataArray = tempArray;
        play.musicIndex = 0;
        play.hidesBottomBarWhenPushed = YES;
        [self presentViewController:play animated:YES completion:nil];
    }
    
}



#pragma mark -- 本地台
- (void)didClickedLiveLocal
{
    LJLRadioPageLocalVC *rplvc = [[LJLRadioPageLocalVC alloc] init];
    rplvc.hidesBottomBarWhenPushed = YES;
    rplvc.tabBarController.tabBar.hidden = YES;
    rplvc.provinceCode = 220000;
    rplvc.type = 2;
    [self.navigationController pushViewController:rplvc animated:YES];
}
#pragma mark -- 国家台
- (void)didClickedLiveCountry
{
    LJLRadioPageLocalVC *rplvc = [[LJLRadioPageLocalVC alloc] init];
    rplvc.hidesBottomBarWhenPushed = YES;
    rplvc.tabBarController.tabBar.hidden = YES;
    rplvc.type = 1;
    [self.navigationController pushViewController:rplvc animated:YES];
}
#pragma mark -- 省市台
- (void)didClickedLiveProvince
{
    LJLProvincePageVC *pagevc = [[LJLProvincePageVC alloc] init];
    pagevc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:pagevc animated:YES];
}
#pragma mark -- 网络台
- (void)didClickedLiveNet
{
    LJLRadioPageLocalVC *rplvc = [[LJLRadioPageLocalVC alloc] init];
    rplvc.hidesBottomBarWhenPushed = YES;
    rplvc.tabBarController.tabBar.hidden = YES;
    rplvc.type = 3;
    [self.navigationController pushViewController:rplvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
